package paiement;


import java.util.*;
import java.io.*;

import java.net.URLEncoder;



public class PaymentProcessor {
	
	private String paimentUrl;
	private String x_login;
	private String x_tran_key;
	private String x_version;
	private String x_delim_data;
	private String x_delim_char;
	private String x_relay_response;
	private String x_type;
	private String x_method;
	private String x_card_num;
	private String x_exp_date;
	private String x_amount;
	private String x_description;
	private String x_first_name;
	private String x_last_name;
	private String x_address;
	private String x_state;
	private String x_zip;
	
	public PaymentProcessor(){
		
		//On initialise les variables de class pr eviter l'exception NullPonterException
		this.setPaimentUrl("https://test.authorize.net/gateway/transact.dll");
		this.x_login = "5w5wD9KGX";
		this.x_tran_key = "4v9VMzv4t5Lv3T6D";
		this.x_version = "3.1";
		this.x_delim_data = "TRUE";
		this.x_delim_char = "|";
		this.x_relay_response = "FALSE";
		this.x_type = "AUTH_CAPTURE";
		this.x_method = "CC";
		this.x_card_num = "";
		this.x_exp_date = "";
		this.x_amount = "";
		this.x_description = "";
		this.x_first_name = "";
		this.x_last_name = "";
		this.x_address = "";
		this.x_state = "";
		this.x_zip = "";
	}


	public String doPayment() throws IOException{
		
		

		// By default, this sample code is designed to post to our test server for
		// developer accounts: https://test.authorize.net/gateway/transact.dll
		// for real accounts (even in test mode), please make sure that you are
		// posting to: https://secure.authorize.net/gateway/transact.dll
		String post_url = "https://test.authorize.net/gateway/transact.dll";

		
		Hashtable<String, String> post_values = new Hashtable<String, String>();

		// the API Login ID and Transaction Key must be replaced with valid values
		post_values.put ("x_login", x_login);
		post_values.put ("x_tran_key",x_tran_key);

		post_values.put ("x_version", x_version);
		post_values.put ("x_delim_data", x_delim_data);
		post_values.put ("x_delim_char", x_delim_char);
		post_values.put ("x_relay_response", x_relay_response);

		post_values.put ("x_type", x_type);
		post_values.put ("x_method", x_method);
		post_values.put ("x_card_num", x_card_num);
		post_values.put ("x_exp_date", x_exp_date);

		post_values.put ("x_amount", x_amount);
		post_values.put ("x_description", x_description);

		post_values.put ("x_first_name", x_first_name);
		post_values.put ("x_last_name", x_last_name);
		post_values.put ("x_address", x_address);
		post_values.put ("x_state", x_state);
		post_values.put ("x_zip", x_zip);
		// Additional fields can be added here as outlined in the AIM integration
		// guide at: http://developer.authorize.net

		// This section takes the input fields and converts them to the proper format
		// for an http post.  For example: "x_login=username&x_tran_key=a1B2c3D4"
		StringBuffer post_string = new StringBuffer();
		Enumeration<String> keys = post_values.keys();
		String data = "";
		while( keys.hasMoreElements() ) {
			String key = URLEncoder.encode(keys.nextElement().toString(),"UTF-8");
			String value = URLEncoder.encode(post_values.get(key).toString(),"UTF-8");
			data += "&" + key + "=" + value;
		}
		
		System.out.println(data);
		System.out.println("?es resultats");
		
		Request request = new Request(post_url,data,"post");
		
	    String responseData =  request.getResponse();
	    
	    System.out.println("LA chaine resultats:  "+responseData);
		// split the response into an array
		String [] responses = responseData.split("\\|");

		// The results are output to the screen.
		for(Iterator<String> iter=Arrays.asList(responses).iterator(); iter.hasNext();) {
			System.out.println(iter.next());
		}
		
		System.out.println("result   "+responses[0]);
		return responseData;
	}
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		PaymentProcessor p = new PaymentProcessor();
		p.doPayment();

	}
	
	public String getPaimentUrl() {
		return paimentUrl;
	}
	public void setPaimentUrl(String paimentUrl) {
		this.paimentUrl = paimentUrl;
	}
	public String getX_login() {
		return x_login;
	}
	public void setX_login(String x_login) {
		this.x_login = x_login;
	}
	public String getX_tran_key() {
		return x_tran_key;
	}
	public void setX_tran_key(String x_tran_key) {
		this.x_tran_key = x_tran_key;
	}
	public String getX_version() {
		return x_version;
	}
	public void setX_version(String x_version) {
		this.x_version = x_version;
	}
	public String getX_delim_data() {
		return x_delim_data;
	}
	public void setX_delim_data(String x_delim_data) {
		this.x_delim_data = x_delim_data;
	}
	public String getX_delim_char() {
		return x_delim_char;
	}
	public void setX_delim_char(String x_delim_char) {
		this.x_delim_char = x_delim_char;
	}
	public String getX_relay_response() {
		return x_relay_response;
	}
	public void setX_relay_response(String x_relay_response) {
		this.x_relay_response = x_relay_response;
	}
	public String getX_type() {
		return x_type;
	}
	public void setX_type(String x_type) {
		this.x_type = x_type;
	}
	public String getX_method() {
		return x_method;
	}
	public void setX_method(String x_method) {
		this.x_method = x_method;
	}
	public String getX_card_num() {
		return x_card_num;
	}
	public void setX_card_num(String x_card_num) {
		this.x_card_num = x_card_num;
	}
	public String getX_exp_date() {
		return x_exp_date;
	}
	public void setX_exp_date(String x_exp_date) {
		this.x_exp_date = x_exp_date;
	}
	public String getX_amount() {
		return x_amount;
	}
	public void setX_amount(String x_amount) {
		this.x_amount = x_amount;
	}
	public String getX_description() {
		return x_description;
	}
	public void setX_description(String x_description) {
		this.x_description = x_description;
	}
	public String getX_first_name() {
		return x_first_name;
	}
	public void setX_first_name(String x_first_name) {
		this.x_first_name = x_first_name;
	}
	public String getX_last_name() {
		return x_last_name;
	}
	public void setX_last_name(String x_last_name) {
		this.x_last_name = x_last_name;
	}
	public String getX_address() {
		return x_address;
	}
	public void setX_address(String x_address) {
		this.x_address = x_address;
	}
	public String getX_state() {
		return x_state;
	}
	public void setX_state(String x_state) {
		this.x_state = x_state;
	}
	public String getX_zip() {
		return x_zip;
	}
	public void setX_zip(String x_zip) {
		this.x_zip = x_zip;
	}
	
	/*voici une ligne qui explique cherche l'etat du paiemt a partir de la reponse qu'on recoit
private String search;
public SearchResult(String search){
 this.search = search;
 String result = "le paiement a reussi";
 
 //partie la plus importante.j'ai un resultat et je cherche s'il contient le mot reussi ou pas
//ce qui me permet de savoir l'etat du paiement
 Pattern pattern = Pattern.compile(this.search);
 Matcher  matcher = pattern.matcher(result);
 
 if(matcher.find()){
  System.out.print("trouver");
  return true;
 }else{
  System.out.print("pas trouve");
  return false;
 }

boolean etatDuPaiement = this.SearchResult("reussi");*/


}